package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.dialog.WebProgressDialog;
import com.lanou.dllo.findfood.entity.NewsResult;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 15/10/29.
 */
public class NewsDesiptionActivity extends Activity {
    private WebView detailMessage;
    private TextView detialTitle,newsTime;
    private NetworkImageView detailImg;
    private  int id;
    private  long time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_deatil);
        initView();
        getIntentId();
        initData();
    }

  private void   initView(){
      detailMessage = (WebView) findViewById(R.id.detailMessage);
      detialTitle = (TextView) findViewById(R.id.detialTitle);
      detailImg = (NetworkImageView) findViewById(R.id.detailImg);
      newsTime = (TextView) findViewById(R.id.newsTime);

    }


    public void getIntentId() {
        id = getIntent().getIntExtra("id",10);

    }
    private void initData() {
        final  WebProgressDialog webProgressDialog= new WebProgressDialog(this,R.id.animaImgweb);
        webProgressDialog.show();

        String httpUrl = "http://apis.baidu.com/tngou/info/show";
        String httpArg = "id=" + id;
        String path = httpUrl + "?" + httpArg;
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                NewsResult result = gson.fromJson(response,NewsResult.class);
                webProgressDialog.dismiss();

                    String htmlString = result.getMessage();
                    String mimrTypeString = "text/html";
                    String encoding = "utf-8";
                    detailMessage.loadDataWithBaseURL("fake://not/needed", htmlString, mimrTypeString, encoding, "");
                    detailMessage.setInitialScale(100);
                    detialTitle.setText(result.getTitle());
                     String url = result.getImg();

                     String imgUrl = "http://tnfs.tngou.net/image/"+url;
                      getBitmap(imgUrl, detailImg);
                     long t = result.getTime();
                     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                     newsTime.setText(sdf.format(Long.parseLong(t + "")));

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                error.printStackTrace();
                ;
            }
        }

        ) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };

        VolleySington.getInstance().addRequest(request);


    }
    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }

}
